﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class BangChietTinhAir : AdminPageBase
    {
        protected decimal PriceAdults = 0;
        protected decimal PriceChild = 0;
        protected decimal PriceBaby = 0;
        protected decimal CountItem = 0;
        #region Khai Bao
        protected List<CalculatorInfo> TicketAirs = new List<CalculatorInfo>();
        protected decimal TotalTicketAirPrices = 0;
        protected decimal TotalTicketAirPricesAdults = 0;
        protected decimal TotalTicketAirPricesChild = 0;
        protected decimal TotalTicketAirPricesBaby = 0;

        protected List<GroupTourInfo> GroupAirs = new List<GroupTourInfo>();
        private readonly GroupTourController _grouptourController = new GroupTourController();
        #endregion
        private List<CalculatorInfo> _calculatorList = new List<CalculatorInfo>();
        private readonly CalculatorController _calculatorController = new CalculatorController();
        protected TourInfo tourInfo = new TourInfo();
        private TourInfo _tourInfo = new TourInfo();
        private readonly TourController _tourController = new TourController();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["TourId"]))
            {
                _calculatorList = _calculatorController.ListCalculator().Where(p => p.TourId == int.Parse(Request.QueryString["TourId"])).ToList();
                tourInfo = new TourController().GetTour(int.Parse(Request.QueryString["TourId"]));
              
                _tourInfo = tourInfo;
                #region         GoiHam
                LoadFlightArrivals();
                LoadSchedule();
              
                LoadConcern();
               
                LoadTime();
                RenderForm();
                #endregion
            }
            //if (_calculatorList.Count == 0) return;


        }
        protected int LoadTongNguoi(List<CalculatorInfo> ojb, int loai)
        {

            if (loai == 1) // người lơn
            {
                return Convert.ToInt32(ojb.Sum(p => p.QuantityAdults) != 0 ? (PriceAdults / ojb.Sum(p => p.QuantityAdults)) * CountItem : 0);
            }
            if (loai == 2) // người tre em
            {
                return Convert.ToInt32(ojb.Sum(p => p.QuantityChild) != 0 ? (PriceChild / ojb.Sum(p => p.QuantityChild)) * CountItem : 0);
            }
            if (loai == 3) // người tre nho
            {
                return Convert.ToInt32(ojb.Sum(p => p.QuantityBaby) != 0 ? (PriceBaby / ojb.Sum(p => p.QuantityBaby)) * CountItem : 0);
            }
            return 0;

        }
        private
            void RenderForm()
        {

            if (!String.IsNullOrEmpty(Request.QueryString["TourId"]))
            {
                //load info tour 
                #region load info tour
                lbQuantityAdultsTour.Text = tourInfo.QuantityAdult.ToString();
                lbQuantityChildTour.Text = tourInfo.QuantityChild.ToString();
                lbQuantityBabyTour.Text = tourInfo.QuantityBaby.ToString();
                lbBeginDateTour.Text = tourInfo.BeginDate.ToString("dd/MM/yyyy") + " -- ";
                lbBeginHourTour.Text = tourInfo.BeginDate.Hour.ToString() + ":";
                lbBeginMinuteTour.Text = tourInfo.BeginDate.Minute.ToString() + ": 00";
                lbEndDateTour.Text = tourInfo.EndDate.ToString("dd/MM/yyyy") + " -- ";
                lbEndHourTour.Text = tourInfo.EndDate.Hour.ToString() + ":";
                lbEndMinuteTour.Text = tourInfo.EndDate.Minute.ToString() + ": 00";
                lbTotalDayTour.Text = tourInfo.QuantityDay.ToString();
                lbTotalNightTour.Text = tourInfo.QuantityNight.ToString();
                #endregion
                #region Tinh Tong
                //load info calculator air
                TicketAirs = _calculatorList.Where(p => p.ItemType == 1).ToList();
                PriceAdults = TicketAirs.Sum(p => p.TotalPriceStationAdults);
                PriceChild = TicketAirs.Sum(p => p.TotalPriceStationChild);
                PriceBaby = TicketAirs.Sum(p => p.TotalPriceStationBaby);
                CountItem = TicketAirs.Count();
                TotalTicketAirPrices = PriceAdults + PriceChild + PriceBaby;
                TotalTicketAirPricesAdults = LoadTongNguoi(TicketAirs, 1);
                TotalTicketAirPricesChild = LoadTongNguoi(TicketAirs, 2);
                TotalTicketAirPricesBaby = LoadTongNguoi(TicketAirs, 3);

                //load in group air
                GroupAirs =
                    _grouptourController.ListGroupTour()
                        .Where(p => p.TourId == int.Parse(Request.QueryString["TourId"])).ToList();

                #endregion
            }
        }

        
        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
            //RedirectLink();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            //RedirectLink();
        }

        #region Load Thong Tin co Ban
        protected void SaveData()
        {
            _tourInfo.QuantityAdult = int.Parse(txtQuantityAdultsTour.Text);
            _tourInfo.QuantityChild = int.Parse(txtQuantityChildTour.Text);

            if (txtQuantityBabyTour.Text.Equals(""))
            {
                _tourInfo.QuantityBaby = 0;
            }
            else
            {
                _tourInfo.QuantityBaby = int.Parse(txtQuantityBabyTour.Text);
            }
            if (!String.IsNullOrEmpty(dteBeginDateTour.Value))
            {
                var beginDate = Convert.ToDateTime(dteBeginDateTour.Value);
                _tourInfo.BeginDate = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day, int.Parse(ddlBeginHourTour.SelectedValue), int.Parse(ddlBeginMinuteTour.SelectedValue), 0);
            }
            if (!String.IsNullOrEmpty(dteEndDateTour.Value))
            {
                var endDate = Convert.ToDateTime(dteEndDateTour.Value);
                _tourInfo.EndDate = new DateTime(endDate.Year, endDate.Month, endDate.Day, int.Parse(ddlEndHourTour.SelectedValue), int.Parse(ddlEndMinuteTour.SelectedValue), 0);
            }
            _tourInfo.QuantityDay = int.Parse(txtTotalDayTour.Text);
            _tourInfo.QuantityNight = int.Parse(txtTotalNightTour.Text);


            _tourInfo.UpdatedBy = UserId;
            _tourInfo.UpdatedDate = DateTime.Now;
            _tourController.UpdateTour(_tourInfo);


            Response.Redirect("/Pages/BangChietTinh.aspx?TourId=" + int.Parse(Request.QueryString["TourId"]));
        }
        private void LoadTime()
        {
            for (var i = 0; i < 24; i++)
            {
                //var hour = i < 10 ? "0" + i : i.ToString();
                ddlBeginHourTour.Items.Add(new ListItem(i.ToString(), i.ToString()));
                ddlEndHourTour.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            for (var i = 0; i < 60; i++)
            {
                //var minute = i < 10 ? "0" + i : i.ToString();
                ddlBeginMinuteTour.Items.Add(new ListItem(i.ToString(), i.ToString()));
                ddlEndMinuteTour.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
        }
        private void LoadFlightArrivals()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/FlightArrivals.xml"));
            var dv = ds.Tables["FlightArrivals"].DefaultView;
            ddlFlightArrivalsAir.DataTextField = "Name";
            ddlFlightArrivalsAir.DataValueField = "ID";
            ddlFlightArrivalsAir.DataSource = dv;
            ddlFlightArrivalsAir.DataBind();
            ddlFlightArrivalsAir.Items.Insert(0, new ListItem("--- Chọn lượt bay ---", "-1"));

           
        }
        private void LoadSchedule()
        {
            ddlScheduleAir.DataSource =
                new ScheduleController().ListSchedule()
                    .Where(p => p.Status == (int)Globals.DocumentStatus.Use && p.ScheduleType == 1)
                    .OrderBy(p => p.ScheduleName);

            ddlScheduleAir.DataTextField = "ScheduleName";
            ddlScheduleAir.DataValueField = "ScheduleId";
            ddlScheduleAir.DataBind();

            ddlScheduleAir.Items.Insert(0, new ListItem("--- Chọn lịch trình ---", "-1"));
        }

      
        private void LoadConcern()
        {
            ddlConcernAir.DataSource = new ConcernController().ListConcernByStatus((int)Globals.DocumentStatus.Use).Where(p => p.ConcernType == 1).OrderBy(p => p.ConcernFullName);
            ddlConcernAir.DataTextField = "ConcernFullName";
            ddlConcernAir.DataValueField = "ConcernId";
            ddlConcernAir.DataBind();

            ddlConcernAir.Items.Insert(0, new ListItem("--- Chọn hãng---", "-1"));

        }

       
      
        #endregion

    }
}